package com.cloud.controller.api.file;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloud.base.BaseFileController;
import com.cloud.cache.user.CloudUserUtils;
import com.cloud.controller.data.file.FolderChildrenData;
import com.cloud.controller.data.file.FolderData;
import com.cloud.controller.from.file.FromFolder;
import com.cloud.domain.file.Folder;
import com.cloud.listener.SendEmail;
import com.cloud.param.file.FolderParam;
import com.cloud.service.file.FolderService;
import com.common.utils.tools.result.BaseReturn;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static com.cloud.cache.user.CloudUserUtils.getUserName;


@RestController
@RequestMapping("/api/folder")
public class FolderController extends BaseFileController {

    @Autowired
    FolderService folderService;
    @Autowired
    ApplicationContext applicationContext;



    @RequestMapping("/test")
    @Transactional
    public void test() {
        SendEmail email = new SendEmail("email");
        email.setAddress("北京市---天通苑北---东三旗");
        email.setName("刘源");
        Folder folder = new Folder();
        folder.setFileName("test");
        boolean save = folderService.save(folder);
        applicationContext.publishEvent(email);
    }
    /**
     * 创建文件夹
     */
    @RequestMapping("/create")
    public BaseReturn create(@RequestBody Folder fromFolder) throws Exception {
        if (StringUtils.isBlank(fromFolder.getName())) {
            return BaseReturn.build("500", "文件名不能为空");
        }
        boolean fileExists = isFileExistsByNameOrParentName(fromFolder.getParentId(), fromFolder.getName());
        if(fileExists){
            return BaseReturn.build("500", "文件已存在");
        }
        String userName = getUserName();
        fromFolder.setUserName(userName);
        Long userId = CloudUserUtils.getUserId();
        fromFolder.setUserId(userId);
        Boolean result = folderService.mkdirFolder(fromFolder);
        if (result) {
            return BaseReturn.build("200", "创建成功");
        }
        return BaseReturn.build("500", "创建失败");

    }

    /**
     * 删除文件夹或文件
     *
     * @return
     */
    @RequestMapping("delete")
    public BaseReturn delete(@RequestParam(value = "id", required = false) Long id) throws Exception {

        Boolean fileExistsById = isFileExistsById(id);
        if (fileExistsById == null) {
            return BaseReturn.build("500", "文件名或文件夹id不能为null");
        }
        if(!fileExistsById){
            return BaseReturn.build("500", "该文件夹不存在");
        }
        Boolean result = folderService.deleteFile(id);
        if (result) {
            return BaseReturn.build("200", "删除成功");
        }
        return BaseReturn.build("500", "删除失败");
    }
    @RequestMapping("/getById")
    public BaseReturn getById(Long id){
        Folder byId = folderService.getById(id);
        if(byId == null){
            return BaseReturn.build("500", "暂无数据");
        }
        return BaseReturn.build("200", "查询成功",byId);
    }

    /**
     * 获取上级目录
     *
     * @return*/
    @RequestMapping("/getParentFolder")
    public BaseReturn getParentFolder(@RequestBody FromFolder fromFolder) throws Exception {
        FolderParam folderParam = new FolderParam();
        BeanUtils.copyProperties(fromFolder,folderParam);
        folderParam.setUserId(CloudUserUtils.getUserId());

        IPage<Folder> folderIPage = folderService.getParentFolderByPage(folderParam);
        return BaseReturn.build("200", "查询成功", folderIPage);
    }

    /**
     * 查询文件夹
     *
     * @return
     */
    @RequestMapping("query")
    public BaseReturn query(@RequestBody FromFolder fromFolder) throws Exception {
        FolderParam folderParam = new FolderParam();
        BeanUtils.copyProperties(fromFolder,folderParam);
        folderParam.setUserId(CloudUserUtils.getUserId());
        IPage<Folder> folderIPage = folderService.listChildrenFileByPage(folderParam);
        return BaseReturn.build("200", "查询成功", folderIPage);
    }


    /**
     * 树状图文件夹
     *
     * @return
     */
    @RequestMapping("/treeByElementUi")
    public BaseReturn treeByElementUi() throws Exception {
        //List<FolderData> treeFolderData = folderService.queryTreeFile();
        List<FolderData> treeFolderData = getTreeFile();
        return BaseReturn.build("200", "查询成功", treeFolderData);
    }

    public List<FolderData> getTreeFile() throws Exception {
        List<FolderData> folderDataList = new ArrayList<>();
        List<Folder> files = ListParentFile();
        for (Folder file : files) {
            FolderData folderData = new FolderData();
            folderData.setId(file.getId());
            if (StringUtils.isBlank(file.getName())) {
                folderData.setLabel(file.getFileName());
                folderData.setType(file.getType().getCode());
            }else {
                folderData.setLabel(file.getName());

            }

            List<FolderChildrenData> childrenTreeFile = getChildrenTreeFile(file.getId());
            folderData.setChildren(childrenTreeFile);
            folderDataList.add(folderData);
        }
        return folderDataList;

    }
    public List<FolderChildrenData> getChildrenTreeFile(Long parentId) {
        List<FolderChildrenData> folderChildrenDataList = new ArrayList<>();
        Collection<Folder> childrenFolders = this.ListChildrenFile(parentId);
        for (Folder childrenFolder : childrenFolders) {
            FolderChildrenData folderChildrenData = new FolderChildrenData();
            folderChildrenData.setId(childrenFolder.getId());
            if (StringUtils.isBlank(childrenFolder.getName())) {
                folderChildrenData.setLabel(childrenFolder.getFileName());
                folderChildrenData.setType(childrenFolder.getType().getCode());
            }else {
                folderChildrenData.setLabel(childrenFolder.getName());
            }
            folderChildrenDataList.add(folderChildrenData);
        }
        return folderChildrenDataList;
    }





    /**
     * 修改文件夹名称
     */
    @RequestMapping("rename")
    public BaseReturn rename(@RequestBody Folder fromFolder) throws Exception {

        if(fromFolder.getId() == null){
            return BaseReturn.build("500", "文件名id不能为空");
        }
        if(StringUtils.isBlank(fromFolder.getFileName())){
            return BaseReturn.build("500", "文件名称不能为空");
        }
        boolean fileExists = isFileExistsById(fromFolder.getId());
        if(!fileExists){
            return BaseReturn.build("500", "文件不存在");
        }
        Boolean result = folderService.rename(fromFolder);
        if(result == null){
            return BaseReturn.build("500", "修改失败");
        }
        if (result) {
            return BaseReturn.build("200", "修改成功");
        }
        return BaseReturn.build("500", "修改失败");
    }


}
